/*
 * Window.java
 *
 * Created on January 19, 2008, 12:31 PM
 */
package pokemoneditor;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.filechooser.*;



public class Window extends javax.swing.JFrame {
    
    ArrayList<BufferedImage> img = new ArrayList<BufferedImage>(20); 
    ArrayList<String> imgNames = new ArrayList<String>(20); 
    
    
    BufferedImage currentImage;
    Editable e;
    
    
    
    /** Creates new form Window */
    public Window() {
        
        initComponents();
        unchanging.setListData(loadImgs("Images\\Static"));
        dynamic.setListData(loadImgs("Images\\Dynamic"));
        background.setListData(loadImgs("Images\\Background"));
        selection.setVisible(true);
        extraWindow.setVisible(true);
        selection.pack();
        
        e = new Editable(this,img,imgNames,loadCodes("Images\\codes.txt"));
        editablePanel.add(e);
        e.setBounds(0,0,Editable.WIDTH+1,Editable.HEIGHT+1);
        e.setVisible(true);             
        e.repaint();
        


    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {                          

        extraWindow = new javax.swing.JFrame();
        jTextField1 = new javax.swing.JTextField();
        selection = new javax.swing.JFrame();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        background = new javax.swing.JList();
        unchanging = new javax.swing.JList();
        dynamic = new javax.swing.JList();
        editableScrollPane = new javax.swing.JScrollPane();
        editablePanel = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        File = new javax.swing.JMenu();
        save = new javax.swing.JMenuItem();
        load = new javax.swing.JMenuItem();
        exit = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        showSelect = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        extraWindow.setTitle("Extra Information");
        extraWindow.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        extraWindow.setMinimumSize(new java.awt.Dimension(400, 100));
        extraWindow.setResizable(false);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField1KeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });

        javax.swing.GroupLayout extraWindowLayout = new javax.swing.GroupLayout(extraWindow.getContentPane());
        extraWindow.getContentPane().setLayout(extraWindowLayout);
        extraWindowLayout.setHorizontalGroup(
            extraWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, extraWindowLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)
                .addContainerGap())
        );
        extraWindowLayout.setVerticalGroup(
            extraWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(extraWindowLayout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        selection.setTitle("Section Window");

        jTabbedPane1.setName("Background"); // NOI18N
        jTabbedPane1.setRequestFocusEnabled(false);
        jTabbedPane1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabChanged(evt);
            }
        });

        background.setName("Background"); // NOI18N
        background.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                backgroundValueChanged(evt);
            }
        });
        jTabbedPane1.addTab("Background", background);
        background.getAccessibleContext().setAccessibleName("Background");

        unchanging.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                unchangingValueChanged(evt);
            }
        });
        jTabbedPane1.addTab("Static", unchanging);

        dynamic.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                dynamicValueChanged(evt);
            }
        });
        jTabbedPane1.addTab("Dynamic", dynamic);

        jScrollPane2.setViewportView(jTabbedPane1);
        jTabbedPane1.getAccessibleContext().setAccessibleName("Background");
        jTabbedPane1.getAccessibleContext().setAccessibleDescription("Background");

        javax.swing.GroupLayout selectionLayout = new javax.swing.GroupLayout(selection.getContentPane());
        selection.getContentPane().setLayout(selectionLayout);
        selectionLayout.setHorizontalGroup(
            selectionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 231, Short.MAX_VALUE)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        selectionLayout.setVerticalGroup(
            selectionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 406, Short.MAX_VALUE)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Pokemon Level Editor");
        setBounds(new java.awt.Rectangle(100, 100, 600, 800));

        editablePanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        editablePanel.setAutoscrolls(true);
        editablePanel.setPreferredSize(new java.awt.Dimension(800, 600));
        editablePanel.setRequestFocusEnabled(false);
        editablePanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                editablePanelMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                editablePanelMouseExited(evt);
            }
        });

        javax.swing.GroupLayout editablePanelLayout = new javax.swing.GroupLayout(editablePanel);
        editablePanel.setLayout(editablePanelLayout);
        editablePanelLayout.setHorizontalGroup(
            editablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 798, Short.MAX_VALUE)
        );
        editablePanelLayout.setVerticalGroup(
            editablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 600, Short.MAX_VALUE)
        );

        editableScrollPane.setViewportView(editablePanel);

        File.setText("File");


        save.setText("Save");
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });
        File.add(save);

        load.setText("Load");
        load.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadActionPerformed(evt);
            }
        });
        File.add(load);

        exit.setText("Exit");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });
        File.add(exit);

        jMenuBar1.add(File);

        jMenu1.setText("Window");

        showSelect.setText("Show Select Window");
        showSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showSelectActionPerformed(evt);
            }
        });
        jMenu1.add(showSelect);

        jMenuItem1.setText("Show Extra Window");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(editableScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 802, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(editableScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 604, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }                        

    private void loadActionPerformed(java.awt.event.ActionEvent evt) {                                     

        JFileChooser choose = new JFileChooser();
        choose.setAcceptAllFileFilterUsed(true);
        choose.setFileFilter(new FileNameExtensionFilter(".png",".png"));
        if(choose.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)   
        {
            String fName = choose.getSelectedFile().getPath();
            e.load(fName);
        }
        
        e.repaint();
    }                                    

    private void tabChanged(javax.swing.event.ChangeEvent evt) {                            
        if(e!=null)
            e.setImage(null,getCurrentTab());
    }                           

    private int getCurrentTab(){
        
        String title = jTabbedPane1.getTitleAt(jTabbedPane1.getSelectedIndex());
        int num=0;
        if(title.equals("Background"))
            num = Editable.BACKGROUND;
        if(title.equals("Static"))
            num = Editable.STATIC;
        if(title.equals("Dynamic"))
            num = Editable.DYNAMIC;
        
        return num;
    }
    
    private void showSelectActionPerformed(java.awt.event.ActionEvent evt) {                                           
        selection.setVisible(true);
    }                                          

    private void dynamicValueChanged(javax.swing.event.ListSelectionEvent evt) {                                     
        e.setImage(img.get(imgNames.indexOf((String)(dynamic.getModel().getElementAt(dynamic.getSelectedIndex())))),getCurrentTab());
    }                                    

    private void unchangingValueChanged(javax.swing.event.ListSelectionEvent evt) {                                        
        e.setImage(img.get(imgNames.indexOf((String)(unchanging.getModel().getElementAt(unchanging.getSelectedIndex())))),getCurrentTab());
    }                                       

    private void backgroundValueChanged(javax.swing.event.ListSelectionEvent evt) {                                        
        e.setImage(img.get(imgNames.indexOf((String)(background.getModel().getElementAt(background.getSelectedIndex())))),getCurrentTab());
    }                                       

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {                                     
        System.exit(0);
    }                                    

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {                                     
        e.repaint();
        try{
            JFileChooser choose = new JFileChooser();
            choose.setAcceptAllFileFilterUsed(false);
            choose.setFileFilter(new FileNameExtensionFilter(".png",".png"));
            if(choose.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)   
            {
                e.setLines(false);
                String fName = choose.getSelectedFile().getPath();
                File f = new File(fName+".png");
                BufferedImage i = new BufferedImage(Editable.WIDTH,Editable.HEIGHT,BufferedImage.TYPE_4BYTE_ABGR);
                e.paintComponent(i.createGraphics());
                ImageIO.write(i,"png",f);
                e.save(fName);
                e.setLines(true);
            }
        }catch(IOException ex){}
    }                                    
                             

    private void editablePanelMouseExited(java.awt.event.MouseEvent evt) {                                          

    }                                         

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        extraWindow.setVisible(true);
    }                                          

    private void editablePanelMouseClicked(java.awt.event.MouseEvent evt) {                                           
        
    }                                          

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {                                     
        
    }                                    

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        
    }                                           

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {                                       

    }                                      

    private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {                                        
        e.setExtraInfo(jTextField1.getText());
    }                                       

    private String[] loadImgs(String folder){
        File f = new File(folder);
        
        File[] files = f.listFiles();
        String[] list = null;
        if(files!=null){
            list = new String[files.length];
            
            for(int i=0;i<files.length;i++)
            {
                list[i] = files[i].getName();
                
                try{
                    img.add(ImageIO.read(files[i]));
                    imgNames.add(files[i].getName());
                }catch(java.io.IOException e){
                    System.out.println("File not found");
                }
                
            }            
        }
        return list;        
    }
    
    private int[] loadCodes(String file){
        int[] codes = new int[imgNames.size()];
        try {
            Scanner input = new Scanner(new File(file));
            
            while (input.hasNextLine()) {
                String line = input.nextLine();
                int index = imgNames.indexOf(line.substring(0, line.indexOf(":")));
                codes[index] = Integer.parseInt(line.substring(line.indexOf(":") + 1));
            }
            
        } catch (FileNotFoundException ex) {
            System.out.println("cannot load");
        }
        return codes;
    }
    
    

    
    // Variables declaration - do not modify                     
    private javax.swing.JMenu File;
    private javax.swing.JList background;
    private javax.swing.JList dynamic;
    private javax.swing.JPanel editablePanel;
    private javax.swing.JScrollPane editableScrollPane;
    private javax.swing.JMenuItem exit;
    private javax.swing.JFrame extraWindow;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JMenuItem load;
    private javax.swing.JMenuItem save;
    private javax.swing.JFrame selection;
    private javax.swing.JMenuItem showSelect;
    private javax.swing.JList unchanging;
    // End of variables declaration                   
    
}
